<template>
    <div class="app-container">
        <div class="warp_padding">  
            <div class="head-container">
                <!-- 搜索 -->
                <label id="labelQuery" class="labelQuery">账号: </label>
                <el-input v-model="formDate.teaId" clearable placeholder="输入账号搜索" style="width: 140px;" class="filter-item" @keyup.enter.native="toQuery" @change="toQuery()"/>
                

                <span v-if="device!=='mobile'">
                    <label class="labelQuery">姓名: </label>
                    <el-input v-model="formDate.teaName" clearable placeholder="输入姓名搜索" style="width:140px;" class="filter-item" @keyup.enter.native="toQuery" @change="toQuery()"/>
                    
                    <label class="labelQuery"  style="padding-left:10px;">教师类型: </label>
                    <el-select v-model="formDate.teaType" clearable placeholder="教师类型搜索" @change="toQuery()" style="width:140px">
                        <el-option
                              v-for="item in teaTypes"
                              :key="item.id"
                              :label="item.name"
                              :value="item.id" style="margin-top:5px">
                        </el-option>
                    </el-select>
                    
                    <label class="labelQuery">是否班主任: </label>
                    <el-select v-model="formDate.isClasssponsor" clearable placeholder="是否班主任搜索" @change="toQuery()" style="width:150px">
                        <el-option
                              v-for="item in isClasssponsors"
                              :key="item.id"
                              :label="item.name"
                              :value="item.id" style="margin-top:5px">
                        </el-option>
                    </el-select>
                    
                    <label class="labelQuery">状态: </label>
                    <el-select v-model="formDate.status" clearable placeholder="状态" @change="toQuery()" style="width:80px">
                        <el-option
                              v-for="item in condition"
                              :key="item.id"
                              :label="item.name"
                              :value="item.id">
                        </el-option>
                    </el-select>

                    <label class="labelQuery">县/区: </label>
                    <area-item @getMessage="showMsg" />
                </span>



                <el-button class="filter-item" size="mini" type="primary" icon="el-icon-search" @click="toQuery">搜索</el-button>
                <!-- 新增 -->
                <div style="display: inline-block;margin: 0px 2px;">
                  <el-button
                    v-if="checkPermission(['tea_all','tea_add'])"
                    class="filter-item"
                    size="mini"
                    type="primary"
                    icon="el-icon-plus"
                    @click="add">新增</el-button>
                  <eForm ref="form" :is-add="isAdd" :subject="Subject" :schIds="schIds" :roles="roles"/>
                </div>
                
                <el-button type="warning" class="filter-item"  size="mini" icon="el-icon-upload" @click='navTo' v-if="checkPermission(['stu_all','stu_add'])">上传</el-button>
            </div>


            <!--列表-->
            <el-table :data="lists" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;" max-height="650">
                <el-table-column type="selection" width="30"></el-table-column>
                
                <el-table-column type="index" width="45"></el-table-column>
                
                <el-table-column prop="teaId" label="账号"   :show-overflow-tooltip="true"></el-table-column>           
                <el-table-column prop="teaName" label="姓名"  :show-overflow-tooltip="true"></el-table-column>
                
                <el-table-column prop="sex" label="性别" :formatter="sex">
                </el-table-column>

                <el-table-column prop="mobile" label="手机号" min-width="100"></el-table-column>

                <el-table-column prop="schName" label="所属学校" :show-overflow-tooltip="true"  ></el-table-column>

                <el-table-column prop="areaName" label="所属县/区"  :show-overflow-tooltip="true" ></el-table-column> 

                <el-table-column prop="teaType" label="教师类型"  :formatter="teaType" :show-overflow-tooltip="true">
                </el-table-column>

                <!-- <el-table-column prop="mobile" label="手机号"  width="120"></el-table-column> -->

                <!-- <el-table-column prop="cardId" label="身份证号"  width="200"></el-table-column> -->

                <el-table-column prop="status" label="状态" sortable>
                     <template slot-scope="scope">
                        <el-tag :type="scope.row.status ? 'info' : 'success'">{{ scope.row.status ? '禁用' : '启用' }}</el-tag>
                    </template>
                </el-table-column>


                <el-table-column prop="isClasssponsor" label="班主任"  :formatter="isClasssponsor">
                </el-table-column>

                <!-- <el-table-column prop="createtime" label="创建日期" :show-overflow-tooltip="true" v-if="device!=='mobile'">
                    <template slot-scope="scope">
                      <span>{{ parseTime(scope.row.createtime) }}</span>
                    </template>
                </el-table-column> -->


                <el-table-column label="操作" width="150"  v-if="checkPermission(['tea_all','tea_edit','tea_del'])">
                    <template slot-scope="scope">
                        <el-button size="mini" type="success" v-if="checkPermission(['tea_all','tea_edit'])" @click="handleEdit(scope.$index, scope.row.id)">编辑</el-button>
                        <el-button type="danger" size="mini" v-if="checkPermission(['tea_all','tea_del'])" @click="handleDel(scope.$index, scope.row.id)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
        </div>

        <!--工具条-->
        <el-col :span="24" class="toolbar">
            <el-button type="danger" size="mini" @click="batchRemove" v-if="checkPermission(['tea_all','tea_del'])" :disabled="this.sels.length===0" class="remove">批量删除</el-button>

            <el-pagination background  @size-change="handleSizeChange"  @current-change="handleCurrentChange"  :current-page.sync="formDate.pageNo" :page-count="pageCount" :page-sizes="[10, 20, 30, 50,100]"  layout="total, sizes,prev, pager, next, jumper" :pager-count="pagerCount" :total="total" class="pagination" ></el-pagination>
        </el-col>

    </div>
</template>

<script>


import areaItem from '@/components/area/areaitem'
import checkPermission from '@/utils/permission' 
import { parseTime } from '@/utils/index'
import eForm from './module/form'
import { userTea, userTeaDel, couName, allSchoolList, getRoles } from '@/api/request'
import { editTea } from '@/api/data'
import { mapGetters } from 'vuex'
export default {
    components: { eForm, areaItem },
    computed: {
        ...mapGetters([
          'device'
        ])
    },
    data() {
        return {
            roles:[],
            Subject:[],
            schIds:[],
            pageCount: 0,
            total:0,  
            listLoading: false,
            lists: [],
            sels: [],//列表选中列
            pagerCount:11,   
            formDate:{
                pageNo: 1,  
                pageSize:10,
                userType:'1',
                teaId:'',
                teaName:'',
                teaType:'',
                isClasssponsor:'',  //班主任
                status:'',
                areaCode:'',
                teaUseType:'0'
            },
            isAdd:true,
            teaTypes: [
                {
                    id: '0',
                    name: '正式教师'
                },
                {
                    id: '1',
                    name: '实习教师'
                },
                {
                    id: '2',
                    name: '调度教师'
                },
            ],
            isClasssponsors: [
                {
                    id: '0',
                    name: '班主任'
                },
                {
                    id: '1',
                    name: '不是班主任'
                },
            ],
            condition:[
                {
                    id: '0',
                    name: '启用'
                },
                {
                    id: '1',
                    name: '禁用'
                },
            ]
        }
    },
    mounted:function(){
        this.ready()
        this.init()
    },
    methods: {
        parseTime,
        checkPermission,
        showMsg(tir){
            this.formDate.areaCode = tir;
            this.init()
        },
        sex(row, column) {
            return row.sex == 0 ? '男' : row.sex == 1 ? '女' : '未知';
        },
        isClasssponsor(row, column) {
            return row.isClasssponsor == 0 ? '是' : row.isClasssponsor == 1 ? '否' : '未知';
        },
        teaType(row, column) {
            return row.teaType == 0 ? '正式教师' : row.teaType == 1 ? '实习教师' :  row.teaType == 2 ? '调度教师' :'未知';
        },
        selsChange: function (sels) {
                this.sels = sels;
        },
        handleCurrentChange(val) {
            this.formDate.pageNo = val;
            this.init()
        },
        handleSizeChange(val) {
            this.formDate.pageSize = val
            this.init()
            // console.log(`page ${val}`);
        },
        toQuery(){
            this.init()
        },
        async ready(){
            try {
                let data = await couName()
                this.Subject = data.data
            } catch (e) {
                console.log(e)
            }

        },
        add(){
            this.isAdd = true
            this.$refs.form.form = { 
                id:'',
                teaId: '',
                password: '',
                teaName:'',
                sex: 0, 
                mobile:'',
                teaType:'',  
                isClasssponsor:1,  //班主任
                areaCode:'',
                subjectIds:[],
                classIds:'',    //班主任所在班级
                userType:'1',
                status: 0,
                cardId:'',
                roleIds:[],
                schId:'',
                remark:'',
                email:''
            },
            this.allSch()
            this.allRoles()
            this.$refs.form.dialog = true
            this.$refs.form.show = true
        },
        init(){
            this.listLoading = true
            let params = {...this.formDate}
            userTea(params).then(res => { 
                let { list, total } = res
                this.lists = list
                this.total = total
                this.listLoading = false 
            })
        },
        
        async handleEdit(index, id) {
            this.$refs.form.show = false
            this.allSch()
            this.allRoles()
            try {
                let Result = await editTea(id)
                if (Result.code == 2000) {
                    this.isAdd = false
                    this.$refs.form.dialog = true
                    let teaType = (Result.data.teaType).toString()
                    Result.data.teaType = teaType
                    if (Result.data.subjectIds) {
                        let dataIntArr = []
                        let dataStrArr = Result.data.subjectIds.split(","); 
                        dataIntArr = dataStrArr.map(item => {  
                            return +item;  
                        });  
                        Result.data.subjectIds = dataIntArr
                    }
                    // let cardId  = ''
                    // Result.data.cardId = cardId
                    this.$refs.form.form = Result.data 
                }
               // this.$refs.edit.disabled = false
            } catch (e) {
                console.log(e)
            }
            
        },
        batchRemove(){
            let ids = this.sels.map(item => item.id);
            this.$confirm('确认删除选中的记录吗?', '提示', {
                        type: 'warning'
                    }).then(() => {
                        this.listLoading = true;
                        userTeaDel(ids).then(res => {
                            this.listLoading = false;
                            this.$message({
                                message: '删除成功',
                                type: 'success'
                            });
                            if (this.lists.length == 1 && this.formDate.pageNo != 1) 
                            this.formDate.pageNo--
                            this.init();
                        })

                    }).catch(() => {
            });
        },
        handleDel(index,ID){
            let ids = [];
            ids.unshift(ID)
            this.$confirm('确认删除该记录吗?', '提示', {
                        type: 'warning'
                    }).then(() => {
                        // console.log(ids)
                        this.listLoading = true;
                        userTeaDel(ids).then(res => {
                            this.listLoading = false;
                            this.$message({
                                message: '删除成功',
                                type: 'success'
                            });
                            if (this.lists.length == 1 && this.formDate.pageNo != 1) 
                            this.formDate.pageNo--
                            this.init();
                        })

                    }).catch(() => {
            });
        },
        navTo(){
            this.$router.push({
                path: '/upload-excel/center',
                query: {
                    id: 1,
                    title:'教师信息导入'
                }
            })
        },
        async allSch(){
            try {
                let data = await allSchoolList()
                this.schIds = data.data
            } catch (e) {
                console.log(e)
            }

        },
        async allRoles(){
            let params = {
                "type": 1
            }
            try {
                let data = await getRoles(params)
                this.roles = data

            } catch (e) {
                console.log(e)
            }

        },
       
    },
}

</script>


<style scoped lang="scss">

</style>
